package com.test.handle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.test.entity.Result;
import com.test.enums.ResultEnum;
import com.test.exception.CarException;
import com.test.utils.ResultUtil;

/**
 * 用于处理所有请求抛出的异常
 * @author dinghao
 *
 */
@ControllerAdvice
public class ExceptionHandle {
	
	private final static Logger logger = LoggerFactory.getLogger(CarException.class);

	@ExceptionHandler(value = Exception.class)
	@ResponseBody
	public Result<?> handle(Exception e) {
		/*
		 * 如果是自定义的异常,返回自定义的异常信息
		 */
		if(e instanceof CarException) {
			CarException carException = (CarException) e;
			return ResultUtil.error(carException.getCode(), carException.getMessage());
		}
		/*
		 * 未知异常
		 */
		logger.error("系统异常{}",e);
		return ResultUtil.error(ResultEnum.UNKONW_ERROR);
	}
}
